package com.hb.back.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 用户表
 * @TableName edu_acl_user
 */
@TableName(value ="edu_acl_user")
@Data
public class EduAclUser implements Serializable, UserDetails {
    /**
     * 会员id
     */
    @TableId
    private String id;

    /**
     * 微信openid
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 昵称
     */
    private String nickName;

    /**
     * 用户头像
     */
    private String salt;

    /**
     * 用户签名
     */
    private String token;

    /**
     * 逻辑删除 1（true）已删除， 0（false）未删除
     */
    private Byte isDeleted;

    /**
     * 创建时间
     */
    private Date eduCreate;

    /**
     * 更新时间
     */
    private Date eduModified;

   @TableField(exist = false)
   private List<EduAclRole> roles;
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * 会员id
     */
    public String getId() {
        return id;
    }

    /**
     * 会员id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 微信openid
     */
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    /**
     * 微信openid
     */
    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
     List<SimpleGrantedAuthority> list=new ArrayList<>();
      for (EduAclRole role : roles){
        for (EduAclPermission permission:role.getPermissionList()){
         if (!ObjectUtils.isEmpty(permission.getPermissionValue())){
             SimpleGrantedAuthority simpleGrantedAuthority=new SimpleGrantedAuthority(permission.getPermissionValue());
               list.add(simpleGrantedAuthority);
         }
        }
       }
        return list;
    }

    /**
     * 密码
     */
    public String getPassword() {
        return password;
    }

    /**
     * 密码
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 昵称
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * 昵称
     */
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    /**
     * 用户头像
     */
    public String getSalt() {
        return salt;
    }

    /**
     * 用户头像
     */
    public void setSalt(String salt) {
        this.salt = salt;
    }

    /**
     * 用户签名
     */
    public String getToken() {
        return token;
    }

    /**
     * 用户签名
     */
    public void setToken(String token) {
        this.token = token;
    }

    /**
     * 逻辑删除 1（true）已删除， 0（false）未删除
     */
    public Byte getIsDeleted() {
        return isDeleted;
    }

    /**
     * 逻辑删除 1（true）已删除， 0（false）未删除
     */
    public void setIsDeleted(Byte isDeleted) {
        this.isDeleted = isDeleted;
    }

    /**
     * 创建时间
     */
    public Date getEduCreate() {
        return eduCreate;
    }

    /**
     * 创建时间
     */
    public void setEduCreate(Date eduCreate) {
        this.eduCreate = eduCreate;
    }

    /**
     * 更新时间
     */
    public Date getEduModified() {
        return eduModified;
    }

    /**
     * 更新时间
     */
    public void setEduModified(Date eduModified) {
        this.eduModified = eduModified;
    }
}